<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增新闻(文章新闻,视频新闻)')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-news-add">
            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select name="type" class="form-control m-b" th:with="type=${@dict.getType('news_type')}" onchange="showInput(this.value)">
                        <option value="-1">请选择新闻类型</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">新闻分类</label>
                <div class="col-sm-8">
                    <select name="newsClassId" class="form-control m-b">
                        <option th:each="dict : ${newsClassListClass}" th:text="${dict.name}" th:value="${dict.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">作者：</label>
                <div class="col-sm-8">
                    <input name="author" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group" id="portraitDiv">
                <label class="col-sm-3 control-label">头像：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="portraitUpload">上传头像</button>
                        <div class="layui-upload-list" id="portraitShow">

                        </div>
                    </div>
                    <input id="portrait" name="portrait" class="form-control" type="hidden">
                </div>
            </div>
            <!-- 文章封面上传 -->
            <div class="form-group" id="articleCoversDiv" style="display: none;">
                <label class="col-sm-3 control-label">文章封面：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="aCoversUpload">上传文章封面</button>
                        <div class="layui-upload-list" id="aCoversShow"></div>
                    </div>
                    <input id="articleCovers" name="articleCovers" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group" id="articleContentDiv" style="display: none;">
                <label class="col-sm-3 control-label">文章内容：</label>
                <div class="col-sm-8">
                    <div class="ibox-content no-padding">
                        <div class="summernote">
                        </div>
                    </div>
                    <input id="articleContent" name="articleContent" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group" id="videoCoverDiv" style="display: none;">
                <label class="col-sm-3 control-label">视频封面：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="vCoverUpload">上传视频封面</button>
                        <div class="layui-upload-list" id="vCoverShow">

                        </div>
                    </div>
                    <input id="videoCover" name="videoCover" class="form-control" type="hidden">
                </div>
            </div>
<!--            <div class="form-group" id="videoUrlDiv" style="display: none;">-->
<!--                <label class="col-sm-3 control-label">视频url：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input name="videoUrl" class="form-control" type="text">-->
<!--                </div>-->
<!--            </div>-->
            <div class="form-group" id="videoUrlDiv" style="display: none;">
                <label class="col-sm-3 control-label">视频：</label>
                <div class="col-sm-8">
                    <button type="button" class="layui-btn" id="test1">上传视频</button>
                    <div class="layui-upload-list">
                        <div id="img_header" style="padding: 5px;"></div>
                        <input type="hidden" id="videoUrl" name="videoUrl">
                        <p id="demoText1"></p>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">浏览量：</label>
                <div class="col-sm-8">
                    <input name="numSee" class="form-control" type="text" value="0">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">点赞量：</label>
                <div class="col-sm-8">
                    <input name="likes" class="form-control" type="text" value="0">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">评论量：</label>
                <div class="col-sm-8">
                    <input name="commentNum" class="form-control" type="text" value="0">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" class="form-control" type="text" value="0">
                </div>
            </div>
<!--            <div class="form-group">-->
<!--                <label class="col-sm-3 control-label">发布时间：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <div class="input-group date">-->
<!--                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>-->
<!--                        <input name="releaseAt" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
        </form>
    </div>

    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript">

        $(document).ready(function () {
            $('.summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $editable) {
                        // console.log('onChange:', contents, $editable);
                        var aHTML = $('.summernote').summernote('code');
                        console.log(aHTML);
                        $("#articleContent").val(aHTML);
                    }
                }
            });
        });

        var acNum = 0;
        var prefix = ctx + "jiaqin/news"
        $("#form-news-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-news-add').serialize());
            }
        }
        function showInput(at){
            if(at == -1){
                $("#videoCoverDiv").hide();
                $("#videoUrlDiv").hide();
                $("#articleCoversDiv").hide();
                $("#articleContentDiv").hide();
            }
            if(at == 0){
                $("#videoCoverDiv").hide();
                $("#videoUrlDiv").hide();
                $("#articleCoversDiv").show();
                $("#articleContentDiv").show();
            }
            if(at == 1){
                $("#articleCoversDiv").hide();
                $("#articleContentDiv").hide();
                $("#videoCoverDiv").show();
                $("#videoUrlDiv").show();
            }
        }
        $("input[name='releaseAt']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            autoclose: true
        });

        layui.use('upload', function(){
            var $ = layui.jquery
                ,upload = layui.upload;

            //头像上传
            var uploadInst = upload.render({
                elem: '#portraitUpload'
                ,url: '/common/upload' //改成您自己的上传接口
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#portraitShow').append('<img src="'+ result +'" alt="'+ file.name +'" style="width: 112px; height: 63px;">')
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code > 0){
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    console.log(res);
                    $("#portrait").val(res.realPath);
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#vCoverUpload'
                ,url: '/common/upload' //改成您自己的上传接口
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#vCoverShow').append('<img src="'+ result +'" alt="'+ file.name +'" style="width: 112px; height: 63px;">')
                    });
                }
                ,done: function(res){
                    //如果上传失败
                    if(res.code > 0){
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    console.log(res);
                    $("#videoCover").val(res.realPath);
                }
                ,error: function(){
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function(){
                        uploadInst.upload();
                    });
                }
            });

            //多图片上传
            upload.render({
                elem: '#aCoversUpload'
                ,url: '/common/upload'
                ,multiple: true
                ,before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                        $('#aCoversShow').append('<img src="'+ result +'" alt="'+ file.name +'" style="width: 112px; height: 112px; margin-right: 10px;">')
                    });
                }
                ,done: function(res){
                    //上传完毕
                    console.log(res);
                    if(acNum == 0){
                        $("#articleCovers").val(res.realPath);
                        acNum++;
                    }else{
                        var acUrls = $("#articleCovers").val() + ','+res.realPath;
                        $("#articleCovers").val(acUrls);
                    }
                    console.log($("#articleCovers").val());
                }
            });
        });

        layui.use('upload', function() {
            var $ = layui.jquery
                , upload = layui.upload;

            //视频上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/common/upload' //改成您自己的上传接口
                ,accept: 'file'
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                    $("#img_header").html("<embed src='" + res.url + "' style='width:200px;height:200px;'>");
                    $("#videoUrl").val(res.url);
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText1');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
        })
    </script>
</body>
</html>
